// api/log_api/log_list.go
package log_api

import (
	"github.com/gin-gonic/gin"
	"muziai-server/models"
	"muziai-server/models/ctype"
	"muziai-server/service/common"
	"muziai-server/utils/jwts"
	"muziai-server/utils/res"
)

type LogListRequest struct {
	models.Page
	UserID  uint           `json:"userID" form:"userID"`
	Ip      string         `json:"ip" form:"ip"`
	Addr    string         `json:"addr" form:"addr"`
	Level   ctype.LogLevel `json:"level" form:"level"`   // info warn error
	IsRead  bool           `json:"isRead" form:"isRead"` // 是否已读
	LogType int8           `json:"logType" form:"logType"`
}

type LogListResponse struct {
	models.LogModel
	UserNickname string `json:"userNickname"`
	UserAvatar   string `json:"userAvatar"`
}

func (LogApi) LogListView(c *gin.Context) {

	var cr LogListRequest
	c.ShouldBindQuery(&cr)

	_claims, _ := c.Get("claims")
	claims := _claims.(*jwts.Claims)

	if claims.IsAuth() {
		cr.UserID = claims.UserID
	}

	_list, count, _ := common.QueryList(models.LogModel{
		UserID:  cr.UserID,
		Ip:      cr.Ip,
		Addr:    cr.Addr,
		Level:   cr.Level,
		IsRead:  cr.IsRead,
		LogType: cr.LogType,
	}, common.Option{
		Page:     cr.Page,
		Sort:     "created_at desc",
		Likes:    []string{"title"},
		Preloads: []string{"UserModel"},
	})
	var list = make([]LogListResponse, 0)
	for _, model := range _list {
		list = append(list, LogListResponse{
			LogModel:     model,
			UserNickname: model.UserModel.Nickname,
			UserAvatar:   model.UserModel.Avatar,
		})
	}

	res.OkWithList(c, list, count)

}
